//
//  CommunityDetailViewController.m
//  Fangmm
//
//  Created by ChaoZhou on 16/3/31.
//  Copyright © 2016年 com.fangmm. All rights reserved.
//

#import "CommunityDetailViewController.h"
#import "HttpServer.h"
#import "Community.h"
#import "CommunityOutLineTableViewCell.h"
#import <UIImageView+WebCache.h>
#import "House.h"
#import "NormalCommunityTableViewCell.h"
#import "MoreButtonTableViewCell.h"
#import "CommunityOtherHouseTableViewCell.h"
#import "SurroundCommunityTableViewCell.h"
#import "Utility.h"
#import "SearchViewController.h"
#import "HouseDetailViewController.h"
#import "TwoButtonsPopViewController.h"
#import "WXApi.h"
#import "HouseDetailViewController.h"
#import <MobClick.h>
@interface CommunityDetailViewController ()<UITableViewDataSource,UITableViewDelegate,MoreButtonTableViewCellDelegate,SurroundCommunityTableViewCellDelegate,TwoButtonsViewDelegate>
{
    Community *community;
    NSMutableArray      *surroundingHouseArray;
    NSMutableArray      *surroundingCommunityArray;
    MoreButtonTableViewCell *tracfficMoreCell;
    MoreButtonTableViewCell *lifeMoreCell;
    MoreButtonTableViewCell *descMoreCell;
    MoreButtonTableViewCell *otherHouseMoreCell;
    MoreButtonTableViewCell *surroundingMoreCell;
    
    NormalCommunityTableViewCell *tracfficMessageCell;
    NormalCommunityTableViewCell *lifeMessageCell;
    NormalCommunityTableViewCell *descCell;
    id  shareData;
    

}

@end

@implementation CommunityDetailViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    surroundingHouseArray     = [NSMutableArray array];
    surroundingCommunityArray = [NSMutableArray array];
    UIImage *leftImage = [UIImage imageNamed:@"back-18-36"];
    UIBarButtonItem *leftBar = [[UIBarButtonItem alloc]initWithImage:[leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(jumpToBack)];
    self.navigationItem.leftBarButtonItem = leftBar;
    UIImage *rightImage = [UIImage imageNamed:@"share-36-36"];
    UIBarButtonItem *rightBar = [[UIBarButtonItem alloc]initWithImage:[rightImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(shareMethod)];
    self.navigationItem.rightBarButtonItem = rightBar;
    self.title = @"小区信息";
    [_mainTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [_mainTableView registerNib:[UINib nibWithNibName:@"CommunityOutLineTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"CommunityOutLineTableViewCell"];
    [_mainTableView registerNib:[UINib nibWithNibName:@"NormalCommunityTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"NormalCommunityTableViewCell"];
    [_mainTableView registerNib:[UINib nibWithNibName:@"MoreButtonTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"MoreButtonTableViewCell"];
    [_mainTableView registerNib:[UINib nibWithNibName:@"CommunityOtherHouseTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"CommunityOtherHouseTableViewCell"];
    [_mainTableView registerNib:[UINib nibWithNibName:@"SurroundCommunityTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"SurroundCommunityTableViewCell"];
}
-(void)viewWillAppear:(BOOL)animated
{
    [self queryCommunityDetail];
    [self queryCommunityOtherHouse];
    [self querySurroundCommunity];
    [MobClick beginLogPageView:@"小区详情页"];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [MobClick endLogPageView:@"小区详情页"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 6;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section)
    {
        case 0:
        {
            return 140;
        }
            break;
        case 1:
        {
            if (indexPath.row == 0)
            {
                if (tracfficMoreCell.isopen == YES)
                {
                    return  tracfficMessageCell.contentLabel.frame.size.height + 45 > 120 ? tracfficMessageCell.contentLabel.frame.size.height + 60 : 120;
                }
                else
                {
                    return 120;
                }
            }
            else
            {
                return 30;
            }
        }
            break;
        case 2:
        {
            if (indexPath.row == 0)
            {
                if (lifeMoreCell.isopen == YES)
                {
                    NSLog(@"cell.hight = %f",lifeMessageCell.contentLabel.frame.size.height + 45 > 120 ? lifeMessageCell.contentLabel.frame.size.height + 70 : 120);
                    return  lifeMessageCell.contentLabel.frame.size.height + 45 > 120 ? lifeMessageCell.contentLabel.frame.size.height + 60 : 120;
                }
                else
                {
                    return 120;
                }
            }
            else
            {
                return 30;
            }
        }
            break;
        case 3:
        {
            if (indexPath.row == 0)
            {
                if (descMoreCell.isopen == YES)
                {
                    NSLog(@"cell.heigh = %f",descCell.contentLabel.frame.size.height + 45 > 120 ? descCell.contentLabel.frame.size.height + 45 : 120);
                    return  descCell.contentLabel.frame.size.height + 45 > 120 ? descCell.contentLabel.frame.size.height + 60 : 120;
                }
                else
                {
                    return 120;
                }
            }
            else
            {
                return 30;
            }
        }
            break;
        case 4:
        {
            if (otherHouseMoreCell.isopen == YES)
            {
                if (indexPath.row == 0)
                {
                    return 45;
                }
                else if(indexPath.row == surroundingHouseArray.count +1)
                {
                    return 30;
                }
                else
                {
                    return 150;
                }
            }
            else
            {
                if (indexPath.row == 0)
                {
                    return 45;
                }
                else if (indexPath.row == 1)
                {
                    return 150;
                }
                else
                {
                    return 30;
                }
                
            }
            
        }
            break;
        default:
        {
            if (surroundingMoreCell.isopen == YES)
            {
                if (indexPath.row == 0)
                {
                    return 45;
                }
                else if (indexPath.row == (ceil((double)((surroundingCommunityArray.count)/3))+ 1))
                {
                    return 30;
                }
                else
                {
                    return 130;
                }
            }
            else
            {
                if (indexPath.row == 0)
                {
                    return 45;
                }
                else if (indexPath.row == 1)
                {
                    return 130;
                }
                else
                {
                    return 30;
                }
            }
        }
            break;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section)
    {
        case 0:
        {
            return 1;
        }
            break;
        case 1:
        {
            return 2;
        }
            break;
        case 2:
        {
            return 2;
        }
            break;
        case 3:
        {
            return 2;
        }
            break;
        case 4:
        {
            if (otherHouseMoreCell.isopen == YES)
            {
                if (surroundingHouseArray.count == 0)
                {
                    return 2;
                }
                else
                {
                    NSLog(@"surroundingHouseArray.count=%lu",(unsigned long)surroundingHouseArray.count);
                    return surroundingHouseArray.count + 2;
                }
            }
            else
            {
                if (surroundingHouseArray.count == 0)
                {
                    return 2;
                }
                else
                {
                    return 3;
                }
            }
        }
            break;
        default:
        {
            if (surroundingMoreCell.isopen == YES)
            {
                if (surroundingCommunityArray.count == 0)
                {
                    return 3;
                }
                else
                {
                    return ceil((double)(surroundingCommunityArray.count)/3) + 2;
                }
            }
            else
            {
                return 3;
            }
        }
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section)
    {
        case 0:
        {
            CommunityOutLineTableViewCell *communityOutLineCell = [tableView dequeueReusableCellWithIdentifier:@"CommunityOutLineTableViewCell"];
            [communityOutLineCell.communityImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?q=60",community.cover]] placeholderImage:[UIImage imageNamed:@"Non_HP"]];
            communityOutLineCell.communityName.text = community.name;
            communityOutLineCell.areaLabel.text = [NSString stringWithFormat:@"%d",community.area];
            communityOutLineCell.locationLabel.text = community.location;
            communityOutLineCell.greeningLabel.text = [NSString stringWithFormat:@"%d",community.virescence];
            communityOutLineCell.yearsLabel.text = community.decade;
            return communityOutLineCell;
        }
            break;
        case 1:
        {
            if (indexPath.row == 0)
            {
                tracfficMessageCell = [tableView dequeueReusableCellWithIdentifier:@"NormalCommunityTableViewCell"forIndexPath:indexPath];
                tracfficMessageCell.titleLabel.text = @"小区附近交通信息";
                tracfficMessageCell.contentLabel.text = community.trafficInfos;
                return tracfficMessageCell;
            }
            else
            {
                if (!tracfficMoreCell)
                {
                    tracfficMoreCell = [[[NSBundle mainBundle]loadNibNamed:@"MoreButtonTableViewCell" owner:self options:nil] firstObject];
                    tracfficMoreCell.tag = indexPath.section;
                    tracfficMoreCell.delegate = self;
                }
                return tracfficMoreCell;
            }
            
        }
            break;
        case 2:
        {
            if (indexPath.row == 0)
            {
                lifeMessageCell = [tableView dequeueReusableCellWithIdentifier:@"NormalCommunityTableViewCell"];
                lifeMessageCell.contentLabel.text = community.lifeServices;
                lifeMessageCell.titleLabel.text = @"小区附近生活信息";
                return lifeMessageCell;
            }
            else
            {
                if (!lifeMoreCell)
                {
                    lifeMoreCell = [[[NSBundle mainBundle]loadNibNamed:@"MoreButtonTableViewCell" owner:self options:nil] firstObject];
                    lifeMoreCell.tag = indexPath.section;
                    lifeMoreCell.delegate = self;
                }
                return lifeMoreCell;
            }
        }
            break;
        case 3:
        {
            if (indexPath.row == 0)
            {
                descCell = [tableView dequeueReusableCellWithIdentifier:@"NormalCommunityTableViewCell"forIndexPath:indexPath];
                descCell.contentLabel.text = community.desc;
                descCell.titleLabel.text = @"小区简介";
                return descCell;
            }
            else
            {
                if (!descMoreCell)
                {
                    descMoreCell = [[[NSBundle mainBundle]loadNibNamed:@"MoreButtonTableViewCell" owner:self options:nil] firstObject];
                    descMoreCell.tag = indexPath.section;
                    descMoreCell.delegate = self;
                }
                return descMoreCell;
            }
        }
            break;
        case 4:
        {
            if(surroundingHouseArray.count == 0)
            {
                if (indexPath.row == 0)
                {
                    UITableViewCell *titleCell = [tableView dequeueReusableCellWithIdentifier:@"otherHouseTitleCell"];
                    titleCell.selectionStyle = UITableViewCellSelectionStyleNone;
                    [titleCell setSelectionStyle:UITableViewCellSelectionStyleNone];
                    if (!titleCell)
                    {
                        titleCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"otherHouseTitleCell"];
                        [titleCell setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 45)];
                        titleCell.textLabel.text = @"本小区的其它房源";
                        titleCell.textLabel.font = [UIFont systemFontOfSize:15];
                    }
                    return titleCell;
                }
                else
                {
                    UITableViewCell *rullCell = [tableView dequeueReusableCellWithIdentifier:@"otherHouserullCell"];
                    rullCell.selectionStyle = UITableViewCellSelectionStyleNone;
                    [rullCell setSelectionStyle:UITableViewCellSelectionStyleNone];
                    if (!rullCell)
                    {
                        rullCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"otherHouserullCell"];
                        rullCell.textLabel.text = @"本小区无其它房源数据";
                        rullCell.textLabel.font = [UIFont systemFontOfSize:15];
                    }
                    return rullCell;
                }
            }
            else
            {
                if (otherHouseMoreCell.isopen == YES)
                {
                    if (indexPath.row == 0)
                    {
                        UITableViewCell *titleCell = [tableView dequeueReusableCellWithIdentifier:@"otherHouseTitleCell"];
                        titleCell.selectionStyle = UITableViewCellSelectionStyleNone;
                        [titleCell setSelectionStyle:UITableViewCellSelectionStyleNone];
                        if (!titleCell)
                        {
                            titleCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"otherHouseTitleCell"];
                            [titleCell setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 45)];
                            titleCell.textLabel.text = @"本小区的其它房源";
                            titleCell.textLabel.font = [UIFont systemFontOfSize:15];
                        }
                        return titleCell;
                    }
                    else if (indexPath.row == surroundingHouseArray.count + 1)
                    {
                        
                        if (!otherHouseMoreCell)
                        {
                            otherHouseMoreCell = [[[NSBundle mainBundle]loadNibNamed:@"MoreButtonTableViewCell" owner:self options:nil] firstObject];
                            otherHouseMoreCell.tag = indexPath.section;
                            otherHouseMoreCell.delegate = self;
                        }
                        return otherHouseMoreCell;
                    }
                    else
                    {
                        CommunityOtherHouseTableViewCell *communityOtherHouseCell = [tableView dequeueReusableCellWithIdentifier:@"CommunityOtherHouseTableViewCell"forIndexPath:indexPath];
                        House *house = surroundingHouseArray[indexPath.row - 1];
                        [communityOtherHouseCell.houseImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?q=60",house.cover]] placeholderImage:[UIImage imageNamed:@"Non_HP"]];
                        communityOtherHouseCell.houseName.text = house.community;
                        communityOtherHouseCell.housePrice.text = [NSString stringWithFormat:@"%d",house.singleRentFloor];
                        communityOtherHouseCell.houseDesc.text = [NSString stringWithFormat:@"%@|%@|%d㎡",house.community,[NSString stringWithFormat:@"%d室%d厅%d卫",house.roomNum,house.hallNum,house.toiletNum],house.area];
                        communityOtherHouseCell.leaderLabel.text = house.seeMode;
                        if(house.check == YES)
                        {
                            communityOtherHouseCell.checkLabel.text = @"房妈妈验房保障";
                        }
                        return communityOtherHouseCell;
                    }
                }
                else
                {
                    if (indexPath.row == 0)
                    {
                        UITableViewCell *titleCell = [tableView dequeueReusableCellWithIdentifier:@"otherHouseTitleCell"];
                        titleCell.selectionStyle = UITableViewCellSelectionStyleNone;
                        [titleCell setSelectionStyle:UITableViewCellSelectionStyleNone];
                        if (!titleCell)
                        {
                            titleCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"otherHouseTitleCell"];
                            [titleCell setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 45)];
                            titleCell.textLabel.text = @"本小区的其它房源";
                            titleCell.textLabel.font = [UIFont systemFontOfSize:15];
                        }
                        return titleCell;
                    }
                    else if (indexPath.row == 1)
                    {
                        CommunityOtherHouseTableViewCell *communityOtherHouseCell = [tableView dequeueReusableCellWithIdentifier:@"CommunityOtherHouseTableViewCell"forIndexPath:indexPath];
                        House *house = surroundingHouseArray[0];
                        [communityOtherHouseCell.houseImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?q=60",house.cover]] placeholderImage:[UIImage imageNamed:@"Non_HP"]];
                        communityOtherHouseCell.houseName.text = house.community;
                        communityOtherHouseCell.housePrice.text = [NSString stringWithFormat:@"%d",house.singleRentFloor];
                        communityOtherHouseCell.houseDesc.text = [NSString stringWithFormat:@"%@|%@|%d㎡",house.community,[NSString stringWithFormat:@"%d室%d厅%d卫",house.roomNum,house.hallNum,house.toiletNum],house.area];
                        communityOtherHouseCell.leaderLabel.text = house.seeMode;
                        if(house.check == YES)
                        {
                            communityOtherHouseCell.checkLabel.text = @"房妈妈验房保障";
                        }
                        return communityOtherHouseCell;
                    }
                    else
                    {
                        if (!otherHouseMoreCell)
                        {
                            otherHouseMoreCell = [[[NSBundle mainBundle]loadNibNamed:@"MoreButtonTableViewCell" owner:self options:nil] firstObject];
                            otherHouseMoreCell.tag = indexPath.section;
                            otherHouseMoreCell.delegate = self;
                        }
                        return otherHouseMoreCell;
                    }
                }
                
            }
            
        }
            break;
        default:
        {
            if (surroundingMoreCell.isopen == YES)
            {
                if (surroundingCommunityArray.count == 0)
                {
                    if (indexPath.row == 0)
                    {
                        UITableViewCell *titleCell = [tableView dequeueReusableCellWithIdentifier:@"titleCell"];
                        titleCell.selectionStyle = UITableViewCellSelectionStyleNone;
                        [titleCell setSelectionStyle:UITableViewCellSelectionStyleNone];
                        if (!titleCell)
                        {
                            titleCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"titleCell"];
                            [titleCell setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 45)];
                            titleCell.textLabel.text = @"附近小区";
                            titleCell.textLabel.font = [UIFont systemFontOfSize:15];
                        }
                        return titleCell;
                    }
                    else if (indexPath.row == 1)
                    {
                        UITableViewCell *rullCell = [tableView dequeueReusableCellWithIdentifier:@"rullCell"];
                        [rullCell setSelectionStyle:UITableViewCellSelectionStyleNone];
                        if (!rullCell)
                        {
                            rullCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"rullCell"];
                            rullCell.textLabel.text = @"无附近小区数据";
                            rullCell.textLabel.font = [UIFont systemFontOfSize:15];
                        }
                        return rullCell;
                    }
                    else
                    {
                        if (!surroundingMoreCell)
                        {
                            surroundingMoreCell = [[[NSBundle mainBundle]loadNibNamed:@"MoreButtonTableViewCell" owner:self options:nil] firstObject];
                            surroundingMoreCell.tag = indexPath.section;
                            surroundingMoreCell.delegate = self;
                        }
                        return surroundingMoreCell;
                    }
                }
                else
                {
                    if (indexPath.row == 0)
                    {
                        UITableViewCell *titleCell = [tableView dequeueReusableCellWithIdentifier:@"titleCell"];
                        [titleCell setSelectionStyle:UITableViewCellSelectionStyleNone];
                        if (!titleCell)
                        {
                            titleCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"titleCell"];
                            [titleCell setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 45)];
                            titleCell.textLabel.text = @"附近小区";
                        }
                        return titleCell;
                    }
                    else if (indexPath.row == (ceil((double)(surroundingCommunityArray.count)/3)) + 1)
                    {
                        if (!surroundingMoreCell)
                        {
                            surroundingMoreCell = [[[NSBundle mainBundle]loadNibNamed:@"MoreButtonTableViewCell" owner:self options:nil] firstObject];
                            surroundingMoreCell.tag = indexPath.section;
                            surroundingMoreCell.delegate = self;
                        }
                        return surroundingMoreCell;
                    }
                    else
                    {
                        SurroundCommunityTableViewCell *surroundCommunityCell = [tableView dequeueReusableCellWithIdentifier:@"SurroundCommunityTableViewCell"];
                        surroundCommunityCell.delegate = self;
                        surroundCommunityCell.cellIndex = indexPath.row;
                        [surroundCommunityCell.communityImage_1 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?p=60",((Community*)(surroundingCommunityArray[(indexPath.row - 1)*3])).cover]] placeholderImage:[UIImage imageNamed:@"Non_HP"]];
                        surroundCommunityCell.communityLabel_1.text = ((Community*)(surroundingCommunityArray[(indexPath.row - 1)*3])).name;
                        NSLog(@"");
                        if (surroundingCommunityArray.count >= (indexPath.row - 1)*3 + 2)
                        {
                            [surroundCommunityCell.communityImage_2 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?p=60",((Community*)(surroundingCommunityArray[(indexPath.row - 1)*3+1])).cover]] placeholderImage:[UIImage imageNamed:@"Non_HP"]];
                            surroundCommunityCell.communityLabel_2.text = ((Community*)(surroundingCommunityArray[(indexPath.row - 1)*3+1])).name;
                        }
                        if (surroundingCommunityArray.count >= (indexPath.row - 1)*3 + 3)
                        {
                            [surroundCommunityCell.communityImage_3 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?p=60",((Community*)(surroundingCommunityArray[(indexPath.row - 1)*3+2])).cover]] placeholderImage:[UIImage imageNamed:@"Non_HP"]];
                            surroundCommunityCell.communityLabel_3.text = ((Community*)(surroundingCommunityArray[(indexPath.row - 1)*3+2])).name;
                        }
                        return surroundCommunityCell;
                    }
                }
            }
            else
            {
                if (surroundingCommunityArray.count == 0)
                {
                    if (indexPath.row == 0)
                    {
                        UITableViewCell *titleCell = [tableView dequeueReusableCellWithIdentifier:@"titleCell"];
                        titleCell.selectionStyle = UITableViewCellSelectionStyleNone;
                        [titleCell setSelectionStyle:UITableViewCellSelectionStyleNone];
                        if (!titleCell)
                        {
                            titleCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"titleCell"];
                            [titleCell setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 45)];
                            titleCell.textLabel.text = @"附近小区";
                            titleCell.textLabel.font = [UIFont systemFontOfSize:15];
                        }
                        return titleCell;
                    }
                    else if(indexPath.row == 1)
                    {
                        UITableViewCell *rullCell = [tableView dequeueReusableCellWithIdentifier:@"rullCell"];
                        [rullCell setSelectionStyle:UITableViewCellSelectionStyleNone];
                        if (!rullCell)
                        {
                            rullCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"rullCell"];
                            rullCell.textLabel.text = @"无附近小区数据";
                            rullCell.textLabel.font = [UIFont systemFontOfSize:15];
                        }
                        return rullCell;
                    }
                    else
                    {
                        if (!surroundingMoreCell)
                        {
                            surroundingMoreCell = [[[NSBundle mainBundle]loadNibNamed:@"MoreButtonTableViewCell" owner:self options:nil] firstObject];
                            surroundingMoreCell.tag = indexPath.section;
                            surroundingMoreCell.delegate = self;
                        }
                        return surroundingMoreCell;
                    }
                }
                else
                {
                    if (indexPath.row == 0)
                    {
                        UITableViewCell *titleCell = [tableView dequeueReusableCellWithIdentifier:@"titleCell"];
                        titleCell.selectionStyle = UITableViewCellSelectionStyleNone;
                        [titleCell setSelectionStyle:UITableViewCellSelectionStyleNone];
                        if (!titleCell)
                        {
                            titleCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"titleCell"];
                            [titleCell setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 45)];
                            titleCell.textLabel.text = @"附近小区";
                            titleCell.textLabel.font = [UIFont systemFontOfSize:15];
                        }
                        return titleCell;
                    }
                    else if (indexPath.row == 2)
                    {
                        if (!surroundingMoreCell)
                        {
                            surroundingMoreCell = [[[NSBundle mainBundle]loadNibNamed:@"MoreButtonTableViewCell" owner:self options:nil] firstObject];
                            surroundingMoreCell.tag = indexPath.section;
                            surroundingMoreCell.delegate = self;
                        }
                        return surroundingMoreCell;
                    }
                    else
                    {
                        SurroundCommunityTableViewCell *surroundCommunityCell = [tableView dequeueReusableCellWithIdentifier:@"SurroundCommunityTableViewCell"];
                        surroundCommunityCell.delegate =self;
                        surroundCommunityCell.cellIndex = indexPath.row;
                        [surroundCommunityCell.communityImage_1 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?q=60",((Community*)(surroundingCommunityArray[(indexPath.row - 1)*3])).cover]] placeholderImage:[UIImage imageNamed:@"Non_HP"]];
                        surroundCommunityCell.communityLabel_1.text = ((Community*)(surroundingCommunityArray[(indexPath.row - 1)*3])).name;
                        if (surroundingCommunityArray.count >= (indexPath.row - 1)*3 + 2)
                        {
                            [surroundCommunityCell.communityImage_2 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?q=60",((Community*)(surroundingCommunityArray[(indexPath.row - 1)*3+1])).cover]] placeholderImage:[UIImage imageNamed:@"Non_HP"]];
                            surroundCommunityCell.communityLabel_2.text = ((Community*)(surroundingCommunityArray[(indexPath.row - 1)*3+1])).name;

                        }
                        if (surroundingCommunityArray.count >= (indexPath.row - 1)*3 + 3)
                        {
                            [surroundCommunityCell.communityImage_3 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?q=60",((Community*)(surroundingCommunityArray[(indexPath.row - 1)*3+2])).cover]] placeholderImage:[UIImage imageNamed:@"Non_HP"]];
                            surroundCommunityCell.communityLabel_3.text = ((Community*)(surroundingCommunityArray[(indexPath.row - 1)*3+2])).name;
                        }
                        return surroundCommunityCell;
                    }
                }
            }
        }

    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 4)
    {
        if(otherHouseMoreCell.isopen == NO)
        {
            if (indexPath.row == 1)
            {
                HouseDetailViewController *houseDetailVC = [[HouseDetailViewController alloc]init];
                houseDetailVC.houseId = ((House*)(surroundingHouseArray[0])).houseId;
                [self.navigationController pushViewController:houseDetailVC animated:NO];
            }
        }
        else
        {
            if (indexPath.row < surroundingHouseArray.count + 1)
            {
                HouseDetailViewController *houseDetailVC = [[HouseDetailViewController alloc]init];
                houseDetailVC.houseId = ((House*)(surroundingHouseArray[indexPath.row - 1])).houseId;
                [self.navigationController pushViewController:houseDetailVC animated:NO];
            }
        }
    }

}
#pragma mark - MoreButtonDelegate
-(void)clickMoreButtonWithCell:(MoreButtonTableViewCell *)cell
{
    cell.isopen = !cell.isopen;
    if (cell.isopen)
    {
        [cell.moreButtonView setTitle:@"收起" forState:UIControlStateNormal];
    }
    else
    {
        [cell.moreButtonView setTitle:@"更多" forState:UIControlStateNormal];
    }
    //[_mainTableView reloadSections:[NSIndexSet indexSetWithIndex:cell.tag] withRowAnimation:UITableViewRowAnimationFade];
    [_mainTableView reloadData];
}
#pragma mark - 请求小区详情
-(void)queryCommunityDetail
{
    [HttpServer getCommunityWithCommunityID:self.communityId withSuccessBlock:^(NSDictionary *responseData)
    {
        NSLog(@"请求小区详情=%@",responseData);
        community = [[Community alloc]initWithDic:responseData];
        [_mainTableView reloadData];
    }
    withFailBlock:^(id errorMessage)
    {
        NSLog(@"%@",errorMessage);
    }];
}
#pragma mark - 请求小区的其它房源
-(void)queryCommunityOtherHouse
{
    [HttpServer getHousesWithParams:@{@"communityId":self.communityId,@"app":@"user",@"cityCode":@"3502"} WithSuccessBlock:^(NSDictionary *responseData)
     {
         NSLog(@"请求小区的其它房源=%@",responseData);
         NSArray *responseArray = responseData[@"houses"];
         [surroundingHouseArray removeAllObjects];
         for (NSDictionary *dic in responseArray)
         {
             House *house = [[House alloc]initWithDic:dic[@"house"]];
             NSLog(@"dic=%@",dic);
             NSLog(@"housename = %@",house.community);
             [surroundingHouseArray addObject:house];
         }
         [_mainTableView reloadData];
     }
     withFailBlock:^(id errorMessage)
     {
         NSLog(@"请求小区的其它房源错误%@",errorMessage);
     }];
}
#pragma mark - 请求附近小区
-(void)querySurroundCommunity
{
    [HttpServer getSourroundingCommunityWithCommunityID:self.communityId withSuccessBlock:^(NSDictionary *responseData)
     {
         NSLog(@"请求附近小区=%@",responseData);
         NSArray *responseArray = responseData[@"communities"];
         [surroundingCommunityArray removeAllObjects];
         for (NSDictionary *dic in responseArray)
         {
             Community *communityItem = [[Community alloc]initWithDic:dic];
             [surroundingCommunityArray addObject:communityItem];
         }
         [_mainTableView reloadData];
     }
    withFailBlock:^(id errorMessage)
     {
         NSLog(@"%@",errorMessage);
     }];
}
-(void)jumpToBack
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 分享
-(void)shareMethod
{
    NSLog(@"分享方法");
    [self loadShareDatasWithCommunityId:_communityId];
    TwoButtonsPopViewController *twoButtonsVC = [[TwoButtonsPopViewController alloc]initWithNibName:@"TwoButtonsPopViewController" bundle:nil];

    [twoButtonsVC setDelegate:self];
    [self presentViewController:twoButtonsVC animated:true completion:^{
        twoButtonsVC.sourceView = @"Share";
        [twoButtonsVC.titleLabel setText:@"分享小区"];
        [twoButtonsVC.firstButton setBackgroundImage:[UIImage imageNamed:@"wechat"] forState:UIControlStateNormal];
        [twoButtonsVC.firstLabel setText:@""];
        [twoButtonsVC.secondButton setBackgroundImage:[UIImage imageNamed:@"fb"] forState:UIControlStateNormal];
        [twoButtonsVC.secondLabel setText:@""];
    }];
}
- (WXMediaMessage *)wechateShareData:(NSDictionary *)successDic {
    WXMediaMessage *message = [[WXMediaMessage alloc]init];
    [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:[successDic objectForKey:@"thumbnail"]] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
    } completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
        message.thumbData = data;
    }];
    message.title = [successDic objectForKey:@"title"];
    message.description = [successDic objectForKey:@"description"];
    return message;
}

#pragma mark - Two buttons view delegate
- (void)sharebuttonClickedWithTag:(NSInteger)tag {
    SendMessageToWXReq *shareReq;
    shareReq = [[SendMessageToWXReq alloc]init];
    shareReq.bText = false;
    shareReq.message = [self wechateShareData:shareData];
    WXWebpageObject *webObj = [[WXWebpageObject alloc]init];
    webObj.webpageUrl = [shareData objectForKey:@"link"];
    shareReq.message.mediaObject = webObj;
    switch (tag) {
        case 0:
        {
            shareReq.scene = 0;
            [WXApi sendReq:shareReq];
            [self dismissViewControllerAnimated:true completion:^{
            }];
        }
            break;
        default:
        {
            shareReq.scene = 1;
            [WXApi sendReq:shareReq];
            
            [self dismissViewControllerAnimated:true completion:^{
            }];
        }
            break;
    }
}
- (void)loadShareDatasWithCommunityId:(NSString *)communityId
{
    if (communityId == nil || [communityId isEqual:@""])
    {
    }
    else
    {
        [HttpServer getSharelinksWithType:@"community" sharedInfo:_communityId WithSuccessBlock:^(NSDictionary *successDic)
        {
            shareData = successDic;
        }
        withFailedBlock:^(id failData)
        {
            NSLog(@"%@",failData);
        }];
    }
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)jumpToCommunity:(UIButton *)sender withIndex:(NSInteger)cellIndex
{
    int communityIndex = (int)((cellIndex - 1) * 3 + sender.tag - 1) ;
    if (communityIndex + 1 <= surroundingCommunityArray.count)
    {
        NSString *communityName = ((Community*)(surroundingCommunityArray[communityIndex])).name;
        NSLog(@"%d",communityIndex);
        SearchViewController *searchVC = [[SearchViewController alloc]init];
        searchVC.searchText = communityName;
        [self.navigationController pushViewController:searchVC animated:YES];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
